$().ready(function() {
	validateRule();
    $("#btnAdd").on("click",function(){
        var t=$('input:radio[name=type]:checked').val();
        var str="";
        if(t=='0'){
            str='style="display: none"';
        }
        $("#tab tbody").append('<tr align="center"><td height="45">' +
            '<input type="text" name="spec_value" class="form-control" data-parsley-required="true"></td>' +
            '<td width="2"></td><td  style="display: none"><input type="text" name="spec_alias" class="form-control"></td><td width="2"></td>' +
            '<td '+str+'><button type="button" class="btn btn-default btn-sm mr5"> ' +
            '<img src="/img/empty.bmp" style="height: 20px;width: 20px;"> ' +
            '<input name="spec_picurl" type="hidden" value=""><i class="ti-upload ml5"></i> </button></td>' +
            '<td><i class="up fa fa-arrow-up" style="cursor:pointer;padding: 5px;" title="向上移动"></i>' +
            '<i class="down fa fa-arrow-down" style="cursor:pointer;padding: 5px;" title="向下移动"></i>' +
            '<i class="del fa fa-remove" style="cursor:pointer;padding: 5px;" title="删除"></i></td></tr>');
        binEvent();
    });
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/wx/goodsSpec/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

function binEvent(){
    $("#tab .up").unbind("click").on("click",function(){
        var p=$(this).parent().parent();
        var nextTR = p.prev();
        if (nextTR.length > 0) {
            nextTR.insertAfter(p);
        }
    });
    $("#tab .down").unbind("click").on("click",function(){
        var p=$(this).parent().parent();
        var nextTR = p.next();
        if (nextTR.length > 0) {
            nextTR.insertBefore(p);
        }
    });
    $("#tab .del").unbind("click").on("click",function(){
        var tr=$("#tab tbody").find("tr");
        if (tr.length > 0) {
            $(this).parent().parent().remove();
        }
    });
    $("#tab .btn").unbind("click").on("click",function(){
        var btn=$(this);
        $("#buttom").load( "${base!}/platform/goods/spec/image?w=36&h=36", function( response, status, xhr ) {
            $("#imgUploadOK").unbind("click").on("click",function(){
                btn.find("img").attr("src",$("#imgUploadPicurl").val());
                btn.find("input[type=hidden]").val($("#imgUploadPicurl").val());
                $("#imgUpload").modal("hide");
            });
            $("#imgUpload").modal("show");
        });
    });
}